package com.xana.acg.mikomiko.media;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;

import com.xana.acg.com.utils.DipAndPx;
import com.xana.acg.com.view.recycler.RecyclerAdapter;
import com.xana.acg.com.view.recycler.RecyclerV;
import com.xana.acg.fac.model.anime.IMedia;
import com.xana.acg.mikomiko.R;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class EpisodePopup extends PopupWindow implements RecyclerAdapter.AdapterListener<IMedia>,
        View.OnTouchListener {
    private static final int COMPLETED = 0;
    protected DismissTimerTask mDismissTimerTask;

    private Context mC;
    private LayoutInflater inflater;
    private View contentView;
    private Timer mDismissTimer;

    private RecyclerV recyclerV;


    private Adapter mAdapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == COMPLETED) {
                dismiss();
            }
        }
    };


    public EpisodePopup(Context ctx) {
        super(ctx);
        mC = ctx;
        inflater = (LayoutInflater) mC.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.popup_episode, null);
        setContentView(contentView);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setWidth(DipAndPx.dip2px(mC, 300));
        setOutsideTouchable(true);
        //不设置该属性，弹窗于屏幕边框会有缝隙并且背景不是半透明
        setBackgroundDrawable(contentView.getBackground());
        recyclerV = contentView.findViewById(R.id.recyclerV);
        recyclerV.setOnTouchListener(this);
        recyclerV.setLayoutManager(new GridLayoutManager(mC, 4));
        recyclerV.setAdapter(mAdapter = new Adapter());

        mAdapter.setListener(this);
        for (int i = 0; i < 20; i++) {
            med m = new med();
            m.index = ""+i;
            m.title = "第"+i+"个";
            mAdapter.add(m);
        }


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        startDismissTimer();
        return false;
    }

    static class med implements IMedia{
        String title;
        String index;
        @Override
        public String title() {
            return title;
        }

        @Override
        public String index() {
            return index;
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        startDismissTimer();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        cancelDismissTimer();
    }

    public void startDismissTimer() {
        cancelDismissTimer();
        mDismissTimer = new Timer();
        mDismissTimerTask = new DismissTimerTask();
        mDismissTimer.schedule(mDismissTimerTask, 3000);
    }

    public void cancelDismissTimer() {
        if (mDismissTimer != null) {
            mDismissTimer.cancel();
        }
        if (mDismissTimerTask != null) {
            mDismissTimerTask.cancel();
        }

    }


    public void play(int i){
        RecyclerAdapter.ViewHolder vh =  (RecyclerAdapter.ViewHolder) recyclerV.findViewHolderForAdapterPosition(i);
//        if(vh!=null)
//            onItemClick(vh, null);
    }

    Adapter.VH vh;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onItemClick(RecyclerAdapter.ViewHolder holder, IMedia iMedia) {
        if(vh!=null){
            vh.textView.setActivated(false);
            vh.textView.setTextColor(mC.getColor(R.color.white));
        }

        vh = (Adapter.VH) holder;
        vh.textView.setActivated(true);
        vh.textView.setTextColor(mC.getColor(R.color.colorAccent));
//        if(mEcListener!=null)
//            mEcListener.click(holder.getAdapterPosition());
    }

    @Override
    public void onItemLongClick(RecyclerAdapter.ViewHolder holder, IMedia iMedia) {

    }

    public class DismissTimerTask extends TimerTask {

        @Override
        public void run() {
            Message msg =  handler.obtainMessage();
            msg.what = COMPLETED;
            handler.sendMessage(msg);
        }
    }


    public void setDataList(List list){
        mAdapter.replace(list);
    }

    static class Adapter extends RecyclerAdapter<IMedia>{

        @Override
        protected int getItemViewType(int position, IMedia iMedie) {
            return R.layout.item_episode;
        }

        @Override
        protected RecyclerAdapter.ViewHolder<IMedia> onCreateViewHolder(View view, int viewType) {
            return new VH(view);
        }

        static class VH extends RecyclerAdapter.ViewHolder<IMedia> {
            TextView textView;
            public VH(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.text_index);
            }
            @Override
            protected void onBind(IMedia iMedia) {
                textView.setText(iMedia.index());
            }
        }
    }

    private OnEpisodeClickListener mEcListener;

    public void setmEcListener(OnEpisodeClickListener mEcListener) {
        this.mEcListener = mEcListener;
    }

    public interface OnEpisodeClickListener{
        void click(int i);
    }
}
